package com.indexingsystem.db.mongoDB.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.indexingsystem.boss.Vo.ArticlesTableVo;
import com.indexingsystem.boss.Vo.ArticlesVo;
import com.indexingsystem.boss.entity.ArticleContents;
import com.indexingsystem.boss.entity.Articles;
import com.indexingsystem.boss.entity.AuditLogs;
import com.indexingsystem.boss.entity.DhResourceauditlogs;
import com.indexingsystem.boss.entity.DhTodayStorageInfos;
import com.indexingsystem.boss.entity.ResCategoryRelations;
import com.indexingsystem.boss.entity.ResourceRelations;
import com.indexingsystem.boss.entity.Resources;
import com.indexingsystem.boss.entity.SpecialResourceDatas;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.boss.utils.JsonUtils;
import com.indexingsystem.db.mongoDB.MongoManager;
import com.indexingsystem.db.mongoDB.comon.MongoDBUtils;
import com.indexingsystem.db.mongoDB.comon.MongoTableName;
import com.indexingsystem.db.mongoDB.po.ArticlesResourceMongoVo;
import com.mongodb.BasicDBObject;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;

/**
 * 存储文章结构mongo内存操作实现类.
 * 
 * @author Administrator
 * 
 */
public class ArticlesResourceMongo {

	/**
	 * 入库管理文章资源mongo新增单条数据.
	 * 
	 * @param articlesResourceMongoVo
	 * @throws Exception
	 */
	public static void addArticles(ArticlesResourceMongoVo articlesResourceMongoVo, String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_ARTICLES_RESOURCE + "添加 ------->" + com.alibaba.fastjson.JSON.toJSONString( articlesResourceMongoVo, true ) );
		if (null != articlesResourceMongoVo) {
			Document mainDocument = new Document();
			mainDocument.put( "_id", articlesResourceMongoVo.getResId() );
			mainDocument.put( "resId", articlesResourceMongoVo.getResId() );
			mainDocument.put( "resTypeId", articlesResourceMongoVo.getResTypeId() );
			mainDocument.put( "resTable", articlesResourceMongoVo.getResTable() );
			mainDocument.put( "institutionId", institutionId );

			Document articlesDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getDhArticles() );
			mainDocument.put( "dhArticles", articlesDoc );

			Document resourcesDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getResources() );
			mainDocument.put( "resources", resourcesDoc );

			Document articleContentsDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getDhArticleContents() );
			mainDocument.put( "dhArticleContents", articleContentsDoc );

			Document resourceauditlogsDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getResourceauditlogs() );
			mainDocument.put( "resourceauditlogs", resourceauditlogsDoc );

			Document auditLogsDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getAuditLogs() );
			mainDocument.put( "auditLogs", auditLogsDoc );

			Document todayStorageInfosDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getTodayStorageInfos() );
			mainDocument.put( "todayStorageInfos", todayStorageInfosDoc );

			List<Document> specialResourceDatasDocList = new ArrayList<Document>();
			if (null != articlesResourceMongoVo.getDhSpecialResourceDatasList() && articlesResourceMongoVo.getDhSpecialResourceDatasList().size() > 0) {
				for (SpecialResourceDatas specialResourceDatas : articlesResourceMongoVo.getDhSpecialResourceDatasList()) {
					Document specialResourceDatasDoc = MongoDBUtils.transform2Doc( specialResourceDatas );
					specialResourceDatasDocList.add( specialResourceDatasDoc );
				}
			}
			mainDocument.put( "dhSpecialResourceDatasList", specialResourceDatasDocList );

			List<Document> dhResourceRelationsDocList = new ArrayList<Document>();
			if (null != articlesResourceMongoVo.getDhResourceRelationsList() && articlesResourceMongoVo.getDhResourceRelationsList().size() > 0) {
				for (ResourceRelations resourceRelations : articlesResourceMongoVo.getDhResourceRelationsList()) {
					Document resourceRelationsDoc = MongoDBUtils.transform2Doc( resourceRelations );
					dhResourceRelationsDocList.add( resourceRelationsDoc );
				}
			}
			mainDocument.put( "dhResourceRelationsList", dhResourceRelationsDocList );

			List<Document> resCategoryRelationsDocList = new ArrayList<Document>();
			if (null != articlesResourceMongoVo.getResCategoryRelationsList() && articlesResourceMongoVo.getResCategoryRelationsList().size() > 0) {
				for (ResCategoryRelations resourceCategoryRelations : articlesResourceMongoVo.getResCategoryRelationsList()) {
					Document resCategoryRelationsDoc = MongoDBUtils.transform2Doc( resourceCategoryRelations );
					resCategoryRelationsDocList.add( resCategoryRelationsDoc );
				}
			}
			mainDocument.put( "resCategoryRelationsList", resCategoryRelationsDocList );

			manager.add( mainDocument, MongoTableName.DH_ARTICLES_RESOURCE );
		}
	}

	/**
	 * 入库管理单一修改文章资源mongo修改.
	 * 
	 * @param articlesResourceMongoVo
	 * @throws Exception
	 */
	public static void updateArticles(ArticlesResourceMongoVo articlesResourceMongoVo, String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_ARTICLES_RESOURCE + "添加 ------->" + com.alibaba.fastjson.JSON.toJSONString( articlesResourceMongoVo, true ) );
		if (null != articlesResourceMongoVo) {
			Document mainDocument = new Document();
			mainDocument.put( "resId", articlesResourceMongoVo.getResId() );
			mainDocument.put( "resTypeId", articlesResourceMongoVo.getResTypeId() );
			mainDocument.put( "resTable", articlesResourceMongoVo.getResTable() );
			mainDocument.put( "institutionId", institutionId );

			Document articlesDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getDhArticles() );
			mainDocument.put( "dhArticles", articlesDoc );

			Document resourcesDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getResources() );
			mainDocument.put( "resources", resourcesDoc );

			Document articleContentsDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getDhArticleContents() );
			mainDocument.put( "dhArticleContents", articleContentsDoc );

			Document resourceauditlogsDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getResourceauditlogs() );
			mainDocument.put( "resourceauditlogs", resourceauditlogsDoc );

			Document auditLogsDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getAuditLogs() );
			mainDocument.put( "auditLogs", auditLogsDoc );

			Document todayStorageInfosDoc = MongoDBUtils.transform2Doc( articlesResourceMongoVo.getTodayStorageInfos() );
			mainDocument.put( "todayStorageInfos", todayStorageInfosDoc );

			List<Document> specialResourceDatasDocList = new ArrayList<Document>();
			if (null != articlesResourceMongoVo.getDhSpecialResourceDatasList() && articlesResourceMongoVo.getDhSpecialResourceDatasList().size() > 0) {
				for (SpecialResourceDatas specialResourceDatas : articlesResourceMongoVo.getDhSpecialResourceDatasList()) {
					Document specialResourceDatasDoc = MongoDBUtils.transform2Doc( specialResourceDatas );
					specialResourceDatasDocList.add( specialResourceDatasDoc );
				}
			}
			mainDocument.put( "dhSpecialResourceDatasList", specialResourceDatasDocList );

			List<Document> dhResourceRelationsDocList = new ArrayList<Document>();
			if (null != articlesResourceMongoVo.getDhResourceRelationsList() && articlesResourceMongoVo.getDhResourceRelationsList().size() > 0) {
				for (ResourceRelations resourceRelations : articlesResourceMongoVo.getDhResourceRelationsList()) {
					Document resourceRelationsDoc = MongoDBUtils.transform2Doc( resourceRelations );
					dhResourceRelationsDocList.add( resourceRelationsDoc );
				}
			}
			mainDocument.put( "dhResourceRelationsList", dhResourceRelationsDocList );

			List<Document> resCategoryRelationsDocList = new ArrayList<Document>();
			if (null != articlesResourceMongoVo.getResCategoryRelationsList() && articlesResourceMongoVo.getResCategoryRelationsList().size() > 0) {
				for (ResCategoryRelations resourceCategoryRelations : articlesResourceMongoVo.getResCategoryRelationsList()) {
					Document resCategoryRelationsDoc = MongoDBUtils.transform2Doc( resourceCategoryRelations );
					resCategoryRelationsDocList.add( resCategoryRelationsDoc );
				}
			}
			mainDocument.put( "resCategoryRelationsList", resCategoryRelationsDocList );

			UpdateOptions options = new UpdateOptions().upsert( true );
			Bson bson = Filters.and( Filters.eq( "resId", articlesResourceMongoVo.getResId() ), Filters.eq( "institutionId", institutionId ) );
			Document doc = new Document( "$set", mainDocument );
			manager.updateOne( bson, doc, options, MongoTableName.DH_ARTICLES_RESOURCE );
		}
	}

	/**
	 * 批量删除文章更新mongodb.
	 * 
	 * @param map
	 * @return
	 */
	public static void deleteArticlesByIds(List<String> resIdList, String institutionId) {
		MongoManager manager = new MongoManager();
		Bson bson = Filters.and( Filters.in( "resId", resIdList ), Filters.eq( "institutionId", institutionId ) );
		manager.deleteAll( bson, MongoTableName.DH_ARTICLES_RESOURCE );
	}

	/**
	 * 返回报刊列表
	 * 
	 * @param map
	 * @return
	 */
	public static List<ArticlesTableVo> findArticlesByPage(ArticlesVo articlesVo, String institutionId) {
		MongoManager manager = new MongoManager();
		List<ArticlesTableVo> articlesTableVoList = null;
		LogCvt.debug( MongoTableName.DH_ARTICLES_RESOURCE + "查文章列表 ------->" );
		BasicDBObject basicDBObject = new BasicDBObject();
		// List<Bson> queryList = new ArrayList<Bson>();
		// Bson bson = Filters.eq( "resTable", "Articles" );
		basicDBObject.put( "resTable", "Articles" );
		basicDBObject.put( "institutionId", institutionId );
		// queryList.add( bson );
		if (StringUtils.isNotBlank( articlesVo.getQueryCriteria() )) {
			// Pattern pattern = Pattern.compile( "^.*" +
			// articlesVo.getQueryCriteria() + ".*$", Pattern.CASE_INSENSITIVE
			// );
			// Bson resNameBson = Filters.regex( "resources.resName", pattern );
			Pattern pattern = Pattern.compile( ".*" + articlesVo.getQueryCriteria() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "resources.resName", pattern );
			// queryList.add( resNameBson );
		}
		if (null != articlesVo.getStatus()) {
			// Bson statusBson = Filters.eq( "resources.status",
			// articlesVo.getStatus() );
			basicDBObject.put( "resources.status", articlesVo.getStatus() );
			// queryList.add( statusBson );
		}
		if (StringUtils.isNotBlank( articlesVo.getTerminalCode() )) {
			// Bson terminalCodeBson = Filters.eq(
			// "dhSpecialResourceDatasList.terminalCode",
			// articlesVo.getTerminalCode() );
			// queryList.add( terminalCodeBson );
			basicDBObject.put( "dhSpecialResourceDatasList.terminalCode", articlesVo.getTerminalCode() );
		}
		if (StringUtils.isNotBlank( articlesVo.getAuthor() )) {
			// Bson authorBson = Filters.eq( "dhArticles.author",
			// articlesVo.getAuthor() );
			Pattern pattern = Pattern.compile( ".*" + articlesVo.getAuthor() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "dhArticles.author", pattern );
			// queryList.add( authorBson );
		}
		if (StringUtils.isNotBlank( articlesVo.getSubTitle() )) {
			// Bson subTitleBson = Filters.eq( "dhArticles.subTitle",
			// articlesVo.getSubTitle() );
			Pattern pattern = Pattern.compile( ".*" + articlesVo.getSubTitle() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "dhArticles.subTitle", pattern );
			// queryList.add( subTitleBson );
		}

		// Bson queryBson = Filters.and( queryList );

		// 排序条件
		Document sortDocument = new Document();
		sortDocument.put( "resources.publishDate", -1 );
		// 不显示的字段
		Document fetchFields = new Document();
		fetchFields.put( "_id", 0 );
		fetchFields.put( "dhArticles", 1 );
		fetchFields.put( "resources", 1 );
		List<Document> documentsList = manager.queryPages( MongoTableName.DH_ARTICLES_RESOURCE, basicDBObject, sortDocument, fetchFields,
				articlesVo.getPageNumber(), articlesVo.getPageSize() );
		try {
			if (null != documentsList && documentsList.size() > 0) {
				articlesTableVoList = new ArrayList<ArticlesTableVo>();
				for (Document document : documentsList) {
					ArticlesTableVo articlesTableVo = new ArticlesTableVo();

					Articles dhArticles = new Articles();
					dhArticles = (Articles) MongoDBUtils
							.setFieldValue( dhArticles, Document.parse( JsonUtils.toJson( document.get( "dhArticles" ) ) ) );
					if (null != dhArticles) {
						articlesTableVo.setArticleType( dhArticles.getArticleType() );
						articlesTableVo.setAuthor( dhArticles.getAuthor() );
						articlesTableVo.setCip( dhArticles.getCip() );
						articlesTableVo.setPage( dhArticles.getPage() );
						articlesTableVo.setSubTitle( dhArticles.getSubTitle() );
						articlesTableVo.setTranslator( dhArticles.getTranslator() );
					}

					Resources resources = new Resources();
					resources = (Resources) MongoDBUtils.setFieldValue( resources, Document.parse( JsonUtils.toJson( document.get( "resources" ) ) ) );
					if (null != resources) {
						articlesTableVo.setResId( resources.getResId() );
						articlesTableVo.setThumbnail( resources.getThumbnail() );
						articlesTableVo.setKeyword( resources.getKeyword() );
						articlesTableVo.setPublishDate( resources.getPublishDate() );
						articlesTableVo.setResName( resources.getResName() );
						articlesTableVo.setStatus( resources.getStatus() );
					}

					if (!articlesTableVoList.contains( articlesTableVo )) {
						articlesTableVoList.add( articlesTableVo );
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		LogCvt.debug( "查询 ArticlesTableVo 结果:" + JSON.toJSONString( articlesTableVoList, true ) );
		return articlesTableVoList;
	}

	/**
	 * 返回文章总数量.
	 * 
	 * @param map
	 * @return
	 */
	public static Integer findArticlesMaxCount(ArticlesVo articlesVo, String institutionId) {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_ARTICLES_RESOURCE + "查文章列表总数量 ------->" );
		BasicDBObject basicDBObject = new BasicDBObject();
		basicDBObject.put( "resTable", "Articles" );
		basicDBObject.put( "institutionId", institutionId );
		if (StringUtils.isNotBlank( articlesVo.getQueryCriteria() )) {
			Pattern pattern = Pattern.compile( ".*" + articlesVo.getQueryCriteria() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "resources.resName", pattern );
		}
		if (null != articlesVo.getStatus()) {
			basicDBObject.put( "resources.status", articlesVo.getStatus() );
		}
		if (StringUtils.isNotBlank( articlesVo.getTerminalCode() )) {
			basicDBObject.put( "dhSpecialResourceDatasList.terminalCode", articlesVo.getTerminalCode() );
		}
		if (StringUtils.isNotBlank( articlesVo.getAuthor() )) {
			Pattern pattern = Pattern.compile( ".*" + articlesVo.getAuthor() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "dhArticles.author", pattern );
		}
		if (StringUtils.isNotBlank( articlesVo.getSubTitle() )) {
			Pattern pattern = Pattern.compile( ".*" + articlesVo.getSubTitle() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "dhArticles.subTitle", pattern );
		}

		// 不显示的字段
		Document fetchFields = new Document();
		fetchFields.put( "_id", 0 );
		fetchFields.put( "resId", 1 );
		Integer maxCount = manager.queryAggregateListMaxCount( basicDBObject, MongoTableName.DH_ARTICLES_RESOURCE );

		LogCvt.debug( "查询 findArticlesMaxCount 总数量:" + maxCount );
		return maxCount;
	}

	/**
	 * 通过resId获取文章信息mongo缓存信息.
	 * 
	 * @param resId
	 * @return
	 */
	public static ArticlesResourceMongoVo selectArticlesInfoByresId(String resId, String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		ArticlesResourceMongoVo articlesResourceMongoVo = null;
		if (StringUtils.isNotBlank( resId )) {
			Bson bson = Filters.and( Filters.in( "resId", resId ), Filters.eq( "institutionId", institutionId ) );
			LogCvt.info( MongoTableName.DH_ARTICLES_RESOURCE + "通过resId获取文章信息条件:" + Arrays.asList( bson ).toString() );
			List<Document> documentsList = manager.queryAggregateList( bson, null, MongoTableName.DH_ARTICLES_RESOURCE );
			if (null != documentsList && documentsList.size() > 0) {
				articlesResourceMongoVo = new ArticlesResourceMongoVo();
				for (Document document : documentsList) {
					Resources resources = new Resources();
					resources = (Resources) MongoDBUtils.setFieldValue( resources, Document.parse( JsonUtils.toJson( document.get( "resources" ) ) ) );
					if (null != resources) {
						articlesResourceMongoVo.setResources( resources );
					}

					Articles dhArticles = new Articles();
					dhArticles = (Articles) MongoDBUtils
							.setFieldValue( dhArticles, Document.parse( JsonUtils.toJson( document.get( "dhArticles" ) ) ) );
					if (null != dhArticles) {
						articlesResourceMongoVo.setDhArticles( dhArticles );
					}

					ArticleContents dhArticleContents = new ArticleContents();
					dhArticleContents = (ArticleContents) MongoDBUtils.setFieldValue( dhArticleContents,
							Document.parse( JsonUtils.toJson( document.get( "dhArticleContents" ) ) ) );
					if (null != dhArticleContents) {
						articlesResourceMongoVo.setDhArticleContents( dhArticleContents );
					}

					DhResourceauditlogs resourceauditlogs = new DhResourceauditlogs();
					resourceauditlogs = (DhResourceauditlogs) MongoDBUtils.setFieldValue( resourceauditlogs,
							Document.parse( JsonUtils.toJson( document.get( "resourceauditlogs" ) ) ) );
					if (null != resourceauditlogs) {
						articlesResourceMongoVo.setResourceauditlogs( resourceauditlogs );
					}

					AuditLogs auditLogs = new AuditLogs();
					auditLogs = (AuditLogs) MongoDBUtils.setFieldValue( auditLogs, Document.parse( JsonUtils.toJson( document.get( "auditLogs" ) ) ) );
					if (null != auditLogs) {
						articlesResourceMongoVo.setAuditLogs( auditLogs );
					}

					DhTodayStorageInfos todayStorageInfos = new DhTodayStorageInfos();
					todayStorageInfos = (DhTodayStorageInfos) MongoDBUtils.setFieldValue( todayStorageInfos,
							Document.parse( JsonUtils.toJson( document.get( "todayStorageInfos" ) ) ) );
					if (null != todayStorageInfos) {
						articlesResourceMongoVo.setTodayStorageInfos( todayStorageInfos );
					}

					List<SpecialResourceDatas> dhSpecialResourceDatasList = new ArrayList<SpecialResourceDatas>();
					dhSpecialResourceDatasList = JSONArray.parseArray( JsonUtils.toJson( document.get( "dhSpecialResourceDatasList" ) ),
							SpecialResourceDatas.class );
					if (null != dhSpecialResourceDatasList && dhSpecialResourceDatasList.size() > 0) {
						articlesResourceMongoVo.setDhSpecialResourceDatasList( dhSpecialResourceDatasList );
					}

					List<ResourceRelations> dhResourceRelationsList = new ArrayList<ResourceRelations>();
					dhResourceRelationsList = JSONArray.parseArray( JsonUtils.toJson( document.get( "dhResourceRelationsList" ) ),
							ResourceRelations.class );
					if (null != dhResourceRelationsList && dhResourceRelationsList.size() > 0) {
						articlesResourceMongoVo.setDhResourceRelationsList( dhResourceRelationsList );
					}

					List<ResCategoryRelations> resCategoryRelationsList = new ArrayList<ResCategoryRelations>();
					resCategoryRelationsList = JSONArray.parseArray( JsonUtils.toJson( document.get( "resCategoryRelationsList" ) ),
							ResCategoryRelations.class );
					if (null != resCategoryRelationsList && resCategoryRelationsList.size() > 0) {
						articlesResourceMongoVo.setResCategoryRelationsList( resCategoryRelationsList );
					}
				}
			}
		}
		return articlesResourceMongoVo;
	}

	/**
	 * 通过resId获取文章信息mongo缓存信息.
	 * 
	 * @param resId
	 * @return
	 */
	public static List<ArticlesResourceMongoVo> selectArticlesInfoListByresId(List<String> resIdList, String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		List<ArticlesResourceMongoVo> articlesResourceMongoVoList = null;
		if (null != resIdList && resIdList.size() > 0) {

			BasicDBObject basicDBObject = new BasicDBObject();
			basicDBObject.put( "resTable", "Articles" );
			basicDBObject.put( "institutionId", institutionId );

			if (null != resIdList && resIdList.size() > 0) {
				basicDBObject.put( "resId", new BasicDBObject( "$in", resIdList ) );
			}

			// 排序条件
			Document sortDocument = new Document();
			sortDocument.put( "resources.publishDate", -1 );
			// 不显示的字段
			Document fetchFields = new Document();
			fetchFields.put( "_id", 0 );
			List<Document> documentsList = manager.queryPages( MongoTableName.DH_ARTICLES_RESOURCE, basicDBObject, sortDocument, fetchFields, 1 );
			// Bson bson = Filters.and( Filters.in( "resId", resIdList ),
			// Filters.eq( "institutionId", institutionId ) );
			LogCvt.info( MongoTableName.DH_ARTICLES_RESOURCE + "通过resId获取文章信息条件:" + Arrays.asList( basicDBObject ).toString() );
			// List<Document> documentsList = manager.queryAggregateList( bson,
			// null, MongoTableName.DH_ARTICLES_RESOURCE );
			if (null != documentsList && documentsList.size() > 0) {
				articlesResourceMongoVoList = new ArrayList<ArticlesResourceMongoVo>();
				for (Document document : documentsList) {
					ArticlesResourceMongoVo articlesResourceMongoVo = new ArticlesResourceMongoVo();
					Resources resources = new Resources();
					resources = (Resources) MongoDBUtils.setFieldValue( resources, Document.parse( JsonUtils.toJson( document.get( "resources" ) ) ) );
					if (null != resources) {
						articlesResourceMongoVo.setResources( resources );
					}

					Articles dhArticles = new Articles();
					dhArticles = (Articles) MongoDBUtils
							.setFieldValue( dhArticles, Document.parse( JsonUtils.toJson( document.get( "dhArticles" ) ) ) );
					if (null != dhArticles) {
						articlesResourceMongoVo.setDhArticles( dhArticles );
					}

					ArticleContents dhArticleContents = new ArticleContents();
					dhArticleContents = (ArticleContents) MongoDBUtils.setFieldValue( dhArticleContents,
							Document.parse( JsonUtils.toJson( document.get( "dhArticleContents" ) ) ) );
					if (null != dhArticleContents) {
						articlesResourceMongoVo.setDhArticleContents( dhArticleContents );
					}

					DhResourceauditlogs resourceauditlogs = new DhResourceauditlogs();
					resourceauditlogs = (DhResourceauditlogs) MongoDBUtils.setFieldValue( resourceauditlogs,
							Document.parse( JsonUtils.toJson( document.get( "resourceauditlogs" ) ) ) );
					if (null != resourceauditlogs) {
						articlesResourceMongoVo.setResourceauditlogs( resourceauditlogs );
					}

					AuditLogs auditLogs = new AuditLogs();
					auditLogs = (AuditLogs) MongoDBUtils.setFieldValue( auditLogs, Document.parse( JsonUtils.toJson( document.get( "auditLogs" ) ) ) );
					if (null != auditLogs) {
						articlesResourceMongoVo.setAuditLogs( auditLogs );
					}

					DhTodayStorageInfos todayStorageInfos = new DhTodayStorageInfos();
					todayStorageInfos = (DhTodayStorageInfos) MongoDBUtils.setFieldValue( todayStorageInfos,
							Document.parse( JsonUtils.toJson( document.get( "todayStorageInfos" ) ) ) );
					if (null != todayStorageInfos) {
						articlesResourceMongoVo.setTodayStorageInfos( todayStorageInfos );
					}

					List<SpecialResourceDatas> dhSpecialResourceDatasList = new ArrayList<SpecialResourceDatas>();
					dhSpecialResourceDatasList = JSONArray.parseArray( JsonUtils.toJson( document.get( "dhSpecialResourceDatasList" ) ),
							SpecialResourceDatas.class );
					if (null != dhSpecialResourceDatasList && dhSpecialResourceDatasList.size() > 0) {
						articlesResourceMongoVo.setDhSpecialResourceDatasList( dhSpecialResourceDatasList );
					}

					List<ResourceRelations> dhResourceRelationsList = new ArrayList<ResourceRelations>();
					dhResourceRelationsList = JSONArray.parseArray( JsonUtils.toJson( document.get( "dhResourceRelationsList" ) ),
							ResourceRelations.class );
					if (null != dhResourceRelationsList && dhResourceRelationsList.size() > 0) {
						articlesResourceMongoVo.setDhResourceRelationsList( dhResourceRelationsList );
					}

					List<ResCategoryRelations> resCategoryRelationsList = new ArrayList<ResCategoryRelations>();
					resCategoryRelationsList = JSONArray.parseArray( JsonUtils.toJson( document.get( "resCategoryRelationsList" ) ),
							ResCategoryRelations.class );
					if (null != resCategoryRelationsList && resCategoryRelationsList.size() > 0) {
						articlesResourceMongoVo.setResCategoryRelationsList( resCategoryRelationsList );
					}

					if (!articlesResourceMongoVoList.contains( articlesResourceMongoVo )) {
						articlesResourceMongoVoList.add( articlesResourceMongoVo );
					}
				}
			}
		}
		return articlesResourceMongoVoList;
	}

	/**
	 * 通过多个resId获取文章信息mongo缓存信息.
	 * 
	 * @param resId
	 * @return
	 */
	public static List<Resources> selectArticlesInfoByresIds(List<String> resIds, String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		List<Resources> resourcesList = new ArrayList<Resources>();

		if (null != resIds) {

			Bson bson = Filters.and( Filters.in( "resId", resIds ), Filters.in( "institutionId", institutionId ) );

			LogCvt.info( MongoTableName.DH_ARTICLES_RESOURCE + "通过resId获取文章信息条件:" + Arrays.asList( bson ).toString() );
			List<Document> documentsList = manager.queryAggregateList( bson, null, MongoTableName.DH_ARTICLES_RESOURCE );
			if (null != documentsList && documentsList.size() > 0) {

				for (Document document : documentsList) {
					Resources resources = new Resources();
					resources = (Resources) MongoDBUtils.setFieldValue( resources, Document.parse( JsonUtils.toJson( document.get( "resources" ) ) ) );
					if (null != resources) {
						resourcesList.add( resources );
					}
				}
			}
		}
		return resourcesList;
	}

}